(function () {
    angular.module('vehicleCheckApp').controller('VehicleCtrl',
	
    function VehicleCtrl($scope, $state,$stateParams, _DBSerive,checkInfoService) {
        //查询未提交信息
        _DBSerive.queryNotSubmitList(localStorage.actno).then(function(result){
            console.log(result);
            if(result.rows.length > 0 && result.rows.item(0).notSubmitLists.length > 0){
                   var notSubmitLists = JSON.parse(result.rows.item(0).notSubmitLists);
                   $scope.notSubmitNum = notSubmitLists.length;
            }
        });

        //查询要求整改信息
        _DBSerive.queryReviseList(localStorage.actno).then(function(result){
            console.log(result);
            if(result.rows.length > 0 && result.rows.item(0).changeLists.length > 0){
                var changeLists = JSON.parse(result.rows.item(0).changeLists);
			    $scope.changeListNum = changeLists.length;
            }
        });

		if($stateParams.transporter){
			$scope.checkInfo = checkInfoService.getInfo();
		}else{
			$scope.checkInfo = {};
		}

		$scope.checkInfo.transporter = $stateParams.transporter;
		$scope.checkInfo.actno = localStorage.actno;

		var storecode = localStorage.storecode;

		$scope.checkInfo.submitTime = new Date().getTime();

		_DBSerive.queryWarehouse(storecode).then(function(results){
			$scope.checkInfo.warehouse = results.rows.item(0);
		});
		_DBSerive.queryCarTypes(storecode).then(function(results){
     		var carTypes = [];
			for(var i=0; i<results.rows.length; i++){
				carTypes.push(results.rows.item(i));
			}
			$scope.carTypes = carTypes;

		});
		
		$scope.vehicleSubmit = function(){
			angular.forEach($scope.carTypes,function(item){
				if(item.id == $scope.checkInfo.cartypeId)
					$scope.checkInfo.carType = item;
			});
			checkInfoService.setInfo($scope.checkInfo);
			console.log($scope.checkInfo);
			$state.go('checklist');
		}
		
		$scope.gotoTransporter = function(){
			checkInfoService.addInfo('sealno', $scope.checkInfo.sealno);
			checkInfoService.addInfo('carno', $scope.checkInfo.carno);
			$state.go('transporter');
		}
    });
})();